/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import javax.naming.InitialContext;

/**
 *
 * @author Lino
 */
public class conexionBD {
    
    private static String UserName="root";
    private static String Pass="root";
    private static String BD="jdbc:mysql://localhost/admision";
    
    
    
      public static  List<String>  obtenerDeBD(String tabla, String descripcion)  {
          List <String> salida=new ArrayList <String>();
       
        try
        {
            // Se registra el Driver de MySQL
  //          DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            
    //        Connection conexion = (Connection) DriverManager.getConnection (
    //            BD,UserName, Pass);
            
            
             InitialContext ctx = new InitialContext();
            //The JDBC Data source that we just created
            DataSource ds = (DataSource) ctx.lookup("admision");
            java.sql.Connection conexion=ds.getConnection();
            java.sql.Statement s = (java.sql.Statement) conexion.createStatement();
            
      
            
            String query="";
            if(descripcion!=null){
                query="select * from "+tabla+" where descripcion= '"+descripcion+"'";
            }else {
                query="select * from "+tabla;
            }
            
            ResultSet rs = s.executeQuery (query);
           
            while (rs.next())
            {
               salida.add(rs.getString("valor"));
               
            }
            
            conexion.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
           
        }
        return salida;
    }
      
      
      public static String getParam(String Descripcion){
          List lista =obtenerDeBD("parametros", Descripcion);
          if(lista.size()>0)return (String) lista.get(0);
          return null;
      }
      
      
      
        public static String getParamOS(String Descripcion){
          List lista =obtenerDeBD("OS", Descripcion);
          if(lista.size()>0)return (String) lista.get(0);
          return null;
      }
        
        
        
        public static String[] obtenerTodos(String tipo){
          List<String> lista=obtenerDeBD(tipo,null);
          
            String[] array = new String[lista.size()];
            
            for(int i=0; i<lista.size();i++){
                array[i]=lista.get(i);
            }
            
          return array;
        }
        
        
        
      public static Long getNumOrdServ(){
         Long salida=new Long(0);
       
        try
        {
         
            //DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            
          //  Connection conexion = (Connection) DriverManager.getConnection (
          //      BD,UserName, Pass);
                      
           // Statement s = (Statement) conexion.createStatement();
       
            
            InitialContext ctx = new InitialContext();
            //The JDBC Data source that we just created
            DataSource ds = (DataSource) ctx.lookup("admision");
            java.sql.Connection conexion=ds.getConnection();
            java.sql.Statement s = (java.sql.Statement) conexion.createStatement();
            
            
            String query="select idOS()";
           
            ResultSet rs = s.executeQuery (query);
          
            while (rs.next())
            {
              salida = rs.getLong(1);               
            }
        
            conexion.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
           
        }
        return salida;
      }
      
}
